package leveup.lock;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * JIT优化——锁消除测试
 *
 * @author <a href='https://gitee.com/hzf2281'>codefish</a>
 * @version 1.0.0
 * @since 2022/08/31 下午 05:41
 */
public class LockElimination {

    /**
     * obj对象满足线程封闭特征，通过JIT优化后无视synchronized(obj)
     */
    public static void fun(){
        Object obj = new Object();
        synchronized (obj){
            System.out.println(obj.hashCode());
        }
    }


    public static void main(String[] args) throws Exception {

        ExecutorService threadPool = Executors.newFixedThreadPool(10);

        for (int i = 0; i < 10; i++) {
            threadPool.submit(()->{
                fun();
            });
        }
        Thread.sleep(1000);
        threadPool.shutdown();

    }
}
